﻿using JMcms.Common;
using System;
using System.Web;
using System.Web.SessionState;

namespace JMcms.Web.Plugin.Favorite
{
    public class ajax :IHttpHandler,IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = JMRequest.GetQueryString("action");
            switch (action)
            {
                case "add": //加入收藏
                    user_favorite_add(context);
                    break;
                case "delete": //删除收藏
                    user_favorite_delete(context);
                    break;
                default: //
                    user_favorite_list(context);
                    break;
                
            }
        }

        #region 加入收藏================================
        private void user_favorite_add(HttpContext context)
        {
            string sitepath = JMRequest.GetString("site");
            string channel_name = JMRequest.GetString("channel");
            int article_id = JMRequest.GetInt("id", 0);

            if (string.IsNullOrEmpty(sitepath))
            {
                context.Response.Write("出错了，站点传输参数不正确！");
                return;
            }
            if (article_id == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，参数传输有误！\"}");
                return;
            }
            JMcms.Model.users userModel = new Web.UI.BasePage().GetUserInfo();
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，用户尚未登录或已超时！\"}");
                return;
            }
            //检查该文章是否存在
            JMcms.Model.article artModel = new JMcms.BLL.article().GetModel(1,article_id);
            if (artModel == null)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，文章不存在或已删除！\"}");
                return;
            }
            BLL.favorite bll = new BLL.favorite();
            if (bll.Exists(article_id, userModel.id))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"抱歉，您已收藏，请勿重复收藏！\"}");
                return;
            }
            Model.favorite model = new Model.favorite();
            model.site_path = Utils.DropHTML(sitepath);
            model.channel_name = Utils.DropHTML(channel_name);
            model.article_id = artModel.id;
            model.article_title = artModel.title;
            model.user_id = userModel.id;
            model.user_name = userModel.user_name;
            model.add_time = DateTime.Now;
            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"成功添加到收藏夹！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;


            
        }
        #endregion

        #region 删除收藏================================
        private void user_favorite_delete(HttpContext context)
        {
            //检查用户是否登录
            JMcms.Model.users model = new Web.UI.BasePage().GetUserInfo();
            if (model == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，用户尚未登录或已超时！\"}");
                return;
            }
            string check_id = JMRequest.GetFormString("checkId");
            if (string.IsNullOrEmpty(check_id))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"删除失败，请检查传输参数！\"}");
                return;
            }
            string[] arrId = check_id.Split(',');
            for (int i = 0; i < arrId.Length; i++)
            {
                int id = Utils.StrToInt(arrId[i], 0);
                if (id > 0)
                {
                    new BLL.favorite().Delete(id, model.id);
                }
            }
            context.Response.Write("{\"status\": 1, \"msg\": \"收藏删除成功！\"}");
            return;
   



        }
        #endregion

        #region 显示状态================================
        private void user_favorite_list(HttpContext context)
        {
            int article_id = JMRequest.GetInt("id", 0);
            string title = "收藏";

            //检查用户是否登录
            JMcms.Model.users model = new Web.UI.BasePage().GetUserInfo();
            if (model != null)
            {
                BLL.favorite bll = new BLL.favorite();
                if (bll.Exists(article_id, model.id))
                    title = "已收藏";
            }
            context.Response.Write("document.write('" + title + "');");
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
